<?php

namespace utils\oss;

use OSS\OssClient;
use think\facade\Log;

class AliyunOssService extends OssService
{
    /**
     * 执行上传操作
     * @param $tmpInfo
     * @param $recording
     * @return mixed
     * @throws \Exception
     */
    public static function upload($tmpInfo): mixed
    {
        try {
            // 构建参数
            $endPoint = config('rds.aliyun_oss.end_point');
            $accessKeyId = config('rds.aliyun_oss.access_key');
            $accessKeySecret = config('rds.aliyun_oss.secret_key');
            $bucket = config('rds.aliyun_oss.bucket');
            $isCName = strpos($endPoint, 'aliyuncs.com') > 0 ? false : true;
            // 生成唯一的文件名和路径
            $path = Parent::setPath('_aliyun_', $tmpInfo);
            // 初始化客户端
            $ossAliyunClient = new OssClient($accessKeyId, $accessKeySecret, $endPoint, $isCName);
            // 上传文件
            $result = $ossAliyunClient->uploadFile($bucket, $path, $tmpInfo['tmp_name']);
            // 完整URL
            $url = $result['info']['url'];
        } catch (\Exception $e) {
            Log::record($e->getMessage());
            throw new \Exception('阿里云OSS上传失败');
        }
        return $url;
    }
}